

*=============================================
* Table A.1: Summary statistics
*=============================================

use    "${dataout}MainDataset" , clear

keep if year>=2015 & year<=2017


tab regtype1
/*
	1.regtype1 "Oslo" ///
	2.regtype1 "Suburbs of Oslo" ///
	3.regtype1 "Bergen/Trondh/Stav" ///
	4.regtype1 "Suburbs of Bergen/Trondh/Stav" ///
	5.regtype1 "Five other large cities" ///
	6.regtype1 "Small cities" ///
	7.regtype1 "Other areas" ///
*/


capt drop LargeCity
gen LargeCity= (regtype1==1 | regtype1==3 | regtype1==5) if !missing(regtype1)
tab LargeCity

gen SuburbsLargeCity4= (regtype1==2 | regtype1==4) if !missing(regtype1)
tab SuburbsLargeCity4

gen SmallCity= (regtype1==6) if !missing(regtype1)
tab SmallCity



*== Overwrite ind-work controls for unemployed: from 0 to missing
*== Ensures that summary statistics for ind-work variables only reflect values for those that are working
forvalues i=1/2 {
replace toll`i'=. if employed`i'==0 & couple==1 & toll`i'==0
replace ptl`i'=. if employed`i'==0 & couple==1 & ptl`i'==0
replace time`i'=. if employed`i'==0 & couple==1 & time`i'==0
replace dist`i'=. if employed`i'==0 & couple==1 & dist`i'==0
replace PublicTransitTime_`i'=. if employed`i'==0 & couple==1 & PublicTransitTime_`i'==0
}

	
label variable toll_fam_mean "Road toll to work, mean (NOK)"
label variable toll_fam_sum "Road toll to work, sum (NOK)"
label variable ptl_fam_km_mean "Bus lane, mean (km)"
label variable ptl_fam_km_sum "Bus lane, sum (km)"
label variable BEV_fam_yes "BEV (yes=1)"
label variable BEV_fam_count "Number of BEVs"
label variable ICE_fam_count "Number of ICEVs"
label variable car_fam_count "Number of cars"
label variable car_fam_yes "Car (yes=1)"
label variable children "Children under age 18 (yes=1)"
label variable antpers_i_regstat_famnr "Number of household members"
label variable LargeCity "Large city"
label variable SuburbsLargeCity4 "Suburbs of large city"
label variable SmallCity "Small city"
label variable toll_fam_yes "Road toll to work (yes=1)"
label variable ptl_fam_yes "Bus lane (yes=1)"
label variable ptl_fam_yes_500m "Bus lane $\geq$ 500 metres (yes=1)"
label variable ptl_fam_pct "Bus lane, share"



/***************************************************************/
// Two adult households 
/***************************************************************/


preserve
keep if couple==1

// Generate new ind-work variables
drop toll_fam_mean ptl_fam_km_mean
egen toll_fam_mean = rowmean(toll1 toll2) 
egen ptl_fam_km_mean = rowmean(ptl1 ptl2) 



egen time = rowmean(time1 time2) 
egen dist = rowmean(dist1 dist2) 
egen PublicTransitTime = rowmean(PublicTransitTime_1 PublicTransitTime_2) 

egen wies_average = rowmean(wies1 wies2) 
egen wies_sum = rowtotal(wies1 wies2) , missing
egen wealth_average = rowmean(wealth1 wealth2) 
egen wealth_sum = rowtotal(wealth1 wealth2) , missing
egen employed_average = rowmean(employed1 employed2) 
egen retired_average = rowmean(retired1 retired2) 
egen age_average = rowmean(age1 age2) 


forvalues i=1/2 {
gen NoHighschool`i'= (grputd`i'==1) if !missing(grputd`i')
gen Highschool`i'= (grputd`i'==2) if !missing(grputd`i')
gen CollegeUniversity`i'= (grputd`i'==3 | grputd`i'==4) if !missing(grputd`i')
gen UnknownEducation`i'= (grputd`i'==5) if !missing(grputd`i')
}

local varlist NoHighschool Highschool CollegeUniversity  UnknownEducation
foreach v in `varlist'{
egen `v'_average = rowmean(`v'1 `v'2) 
}


// Labels 
label variable wies_average "Household income, mean (NOK)"
label variable wies_sum "Household income, sum (NOK)"
label variable wealth_average "Household wealth, mean (NOK)"
label variable wealth_sum "Household wealth, sum (NOK)"
label variable employed_average "Employment (yes=1)"
label variable retired_average "Retired (yes=1)"
label variable age_average "Age"
label variable NoHighschool_average "No high school"
label variable Highschool_average "High school"
label variable CollegeUniversity_average "University/College"
label variable UnknownEducation_average "Unknown education"

label variable dist "Distance to work (km)"
label variable time "Time to work, driving (min)"
label variable PublicTransitTime "Time to work, public transit (min)"

label variable toll_fam_mean "Road toll to work, mean (NOK)"
label variable ptl_fam_km_mean "Bus lane, mean (km)"


local varlist1 BEV_fam_yes BEV_fam_count ICE_fam_count car_fam_count car_fam_yes 
eststo :  estpost summarize `varlist1' /*, listwise*/
estimates save "${ster}WP2_b_summary_DepVar_couple1_2015_2017" , replace


local varlist2 toll_fam_mean toll_fam_sum toll_fam_yes ptl_fam_km_mean ptl_fam_km_sum ptl_fam_yes ptl_fam_yes_500m ptl_fam_pct dist time  PublicTransitTime
eststo :  estpost summarize `varlist2' /*, listwise*/
estimates save "${ster}WP2_b_summary_TravelToWork_couple1_2015_2017" , replace


local varlist3 antpers_i_regstat_famnr children  wies_average wies_sum wealth_average wealth_sum employed_average retired_average  age_average NoHighschool_average Highschool_average CollegeUniversity_average  LargeCity SuburbsLargeCity4 SmallCity
eststo :  estpost summarize `varlist3'  /*, listwise*/
estimates save "${ster}WP2_b_summary_HHchar_couple1_2015_2017" , replace


tempfile helpfile1
save `helpfile1'
restore


/***************************************************************/
// Single adult households 
/***************************************************************/


preserve
keep if couple==0


/********** Generate one value per row values *******************************/
egen employed = rowmax(employed1 employed2) 
egen retired = rowmax(retired1 retired2) 
egen age = rowmax(age1 age2) 
egen wies = rowmax(wies1 wies2) 
egen wealth = rowmax(wealth1 wealth2) 
egen grputd = rowmax(grputd1 grputd2) 
egen time = rowmax(time1 time2) 
egen dist = rowmax(dist1 dist2) 
egen grk_bed_num  = rowmax(grk_bed1_num  grk_bed2_num) 
egen PublicTransitTime  = rowmax(PublicTransitTime_1  PublicTransitTime_2) 
/****************************************************************************/

// Generate new variables
gen wies_average = wies
gen wies_sum = wies
gen wealth_average = wealth
gen wealth_sum = wealth
gen employed_average = employed
gen retired_average = retired
gen age_average = age

gen NoHighschool_average= (grputd==1) if !missing(grputd)
gen Highschool_average= (grputd==2) if !missing(grputd)
gen CollegeUniversity_average= (grputd==3 | grputd==4) if !missing(grputd)
gen UnknownEducation_average= (grputd==5) if !missing(grputd)


local varlist1 BEV_fam_yes BEV_fam_count ICE_fam_count car_fam_count car_fam_yes 
eststo :  estpost summarize `varlist1' /*, listwise*/
estimates save "${ster}WP2_b_summary_DepVar_couple0_2015_2017" , replace

local varlist2 toll_fam_mean toll_fam_sum toll_fam_yes ptl_fam_km_mean ptl_fam_km_sum ptl_fam_yes ptl_fam_yes_500m ptl_fam_pct dist time  PublicTransitTime
eststo :  estpost summarize `varlist2' /*, listwise*/
estimates save "${ster}WP2_b_summary_TravelToWork_couple0_2015_2017" , replace


local varlist3  antpers_i_regstat_famnr children wies_average wies_sum wealth_average wealth_sum employed_average retired_average  age_average NoHighschool_average Highschool_average CollegeUniversity_average  LargeCity SuburbsLargeCity4 SmallCity
eststo :  estpost summarize `varlist3'  /*, listwise*/
estimates save "${ster}WP2_b_summary_HHchar_couple0_2015_2017" , replace


tempfile helpfile0
save `helpfile0'
restore


use `helpfile1' , clear
append using `helpfile0'


	

*======================================
// Make summary tables 
*======================================


	// Dep variables 
	eststo drop *
	estimates use "${ster}WP2_b_summary_DepVar_couple1_2015_2017" 
	eststo 
	estimates use "${ster}WP2_b_summary_DepVar_couple0_2015_2017" 
	eststo
	esttab *  using "${tables}TableA1_PanelA.tex" , ///
	cells("mean(fmt(%12.3fc)) sd(fmt(%12.3fc)) ") ///
	label noobs   nomtitle  nonumber  fragment  collabels(none) nolines replace
	
	
	// Travel to work variables
	eststo drop *
	estimates use "${ster}WP2_b_summary_TravelToWork_couple1_2015_2017" 
	eststo 
	estimates use "${ster}WP2_b_summary_TravelToWork_couple0_2015_2017" 
	eststo 
	esttab *  using "${tables}TableA1_PanelB.tex" , ///
	cells("mean(fmt(%12.2fc)) sd(fmt(%12.2fc)) ") ///
	label  noobs  nomtitle  nonumber  fragment  collabels(none) nolines replace

	
	// HH variables
	eststo drop *
	estimates use "${ster}WP2_b_summary_HHchar_couple1_2015_2017" 
	eststo 
	estimates use "${ster}WP2_b_summary_HHchar_couple0_2015_2017" 
	eststo 
	esttab *  using "${tables}TableA1_PanelC.tex" , ///
	cells("mean(fmt(%12.2fc)) sd(fmt(%12.2fc)) ") ///
	label   nomtitle  nonumber  fragment  collabels(none) nolines replace
	
